package nccloud.web.pu.buyingreq.action;

import nc.vo.pu.m20.entity.PraybillItemVO;
import nc.vo.pu.m20.entity.PraybillSupplierVO;
import nc.vo.pu.m20.entity.PraybillVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pflow.PfUserObject;
import nc.vo.pubapp.pub.power.PowerActionEnum;
import nc.vo.scmpub.exp.SaveAndCommitException;
import nc.vo.scmpub.res.billtype.POBillType;
import nccloud.dto.scmpub.script.entity.SCMScriptResultDTO;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.core.json.IJson;
import nccloud.framework.core.util.GridCompareUtils;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.ui.pattern.billcard.BillCard;
import nccloud.framework.web.ui.pattern.billcard.BillCardOperator;
import nccloud.framework.web.ui.pattern.extbillcard.ExtBillCard;
import nccloud.framework.web.ui.pattern.extbillcard.ExtBillCardOperator;
import nccloud.pubitf.scmpub.pub.service.ISCMPubSaveCommitService;
import nccloud.web.pu.buyingreq.entity.QueryInfoBuyingreq;
import nccloud.web.pu.buyingreq.util.BuyingReqScaleUtil;
import nccloud.web.scmpub.pub.action.DataPermissionAction;
import nccloud.web.scmpub.pub.assign.CommitAssignUtils;
import nccloud.web.scmpub.pub.resexp.PfResumeExceptionNccUtils;

public class SaveAndCommitAction extends DataPermissionAction {
	public Object doAction(IRequest request) {

		ExtBillCard card = null;
		try {
			CommitAssignUtils.before(request);

			PfResumeExceptionNccUtils.before(request);
			String read = request.read();
			IJson json = JsonFactory.create();

			QueryInfoBuyingreq query = (QueryInfoBuyingreq) json.fromJson(read, QueryInfoBuyingreq.class);
			String type = query.getType();

			ExtBillCardOperator extOperator = new ExtBillCardOperator();
			BillCardOperator operator = new BillCardOperator();
			PraybillVO vo = (PraybillVO) extOperator.toBill(request);
			BillCard oldCard = operator.toCardBySpecialBodyArea(vo, "card_body");
			afterProcess(oldCard);

//			//校验请购单是否输入供应商行
//			if (!checkSupplier(vo)) {
//				ExceptionUtils.wrapException(new BusinessException("请填写供应商信息!"));
//			}
			
			String pk_id = vo.getHVO().getPk_praybill();

			if (pk_id != null && vo.getParent().getStatus() != 2) {
				vo.getParent().setStatus(1);
				setDefaultValue(vo);
			} else {
				vo.getParent().setStatus(2);
			}

			PfUserObject pfUserObject = new PfUserObject();

			ISCMPubSaveCommitService service = (ISCMPubSaveCommitService) ServiceLocator
					.find(ISCMPubSaveCommitService.class);
			SCMScriptResultDTO scriptResult = service.saveCommit(new PraybillVO[] { vo }, pfUserObject,
					PraybillVO.class, "SAVEBASE", "SAVE", POBillType.PrayBill

							.getCode());
			card = extOperator.toCard(scriptResult.getSucessVOs()[0]);
			BillCard billCard = operator.toCardBySpecialBodyArea(scriptResult.getSucessVOs()[0], "card_body");
			afterProcess(billCard);

			if (type != null) {
				return card;
			}
			card = GridCompareUtils.compareExtBillCardGrid(extOperator.getOriginBillcard(), card);
		} catch (Exception e) {
			Throwable ex = ExceptionUtils.unmarsh(e);
			if (ex instanceof SaveAndCommitException) {
				return ((SaveAndCommitException) ex).getObject();
			}

			return PfResumeExceptionNccUtils.handleResumeException(e);
		}
		return card;
	}

	public void setDefaultValue(PraybillVO vo) {
		String pk = vo.getHVO().getPk_praybill();
		for (PraybillItemVO item : vo.getBVO()) {
			if (2 == item.getStatus()) {
				item.setPk_praybill(pk);
				item.setPk_praybill_b(null);
			}
		}
	}

	public String getPermissioncode() {
		return POBillType.PrayBill.getCode();
	}

	public String getActioncode() {
		return PowerActionEnum.COMMIT.getActioncode();
	}

	public String getBillCodeField() {
		return "vbillcode";
	}

	public void afterProcess(BillCard card) {
		BuyingReqScaleUtil buyUtil = new BuyingReqScaleUtil();
		buyUtil.processBillCard(card, false);
	}
	
	public boolean checkSupplier(PraybillVO vo) {
		PraybillItemVO[] itemVOs = (PraybillItemVO[]) vo.getChildren(PraybillItemVO.class);
		for (PraybillItemVO itemVO : itemVOs) {
			if (itemVO.getCsourceid() != null && !"".equals(itemVO.getCsourceid())) {
				return true;
			}
		}
		
		PraybillSupplierVO[] supplierVOs = (PraybillSupplierVO[]) vo.getChildren(PraybillSupplierVO.class);
		if (supplierVOs == null || supplierVOs.length == 0) {
			return false;
		}
		for (PraybillSupplierVO supplierVO : supplierVOs) {
			if (supplierVO == null || supplierVO.getPk_supplier_v() == null || "".equals(supplierVO.getPk_supplier_v())) {
				return false;
			}
		}
		return true;
	};
}
